<?php

namespace App\Jobs;

use App\Exceptions\Arr;
use App\Imports\HospitalImport;
use App\Models\Appoint;
use App\Models\AppointNum;
use App\Models\EbHospitalUploadData;
use App\Models\HospitalUploadData;
use App\Models\HospitalUserFamily;
use App\Models\Notice;
use App\Models\TempPushRecord;
use App\Tools\EnvironmentVariableCredentialsProvider;
use App\Wechat\Temp;
use common\models\ChildInfo;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use OSS\OssClient;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use Maatwebsite\Excel\Facades\Excel;

class HospitalUpload implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $hospitalUploadData;
    public $timeout = 999999999; // 设置一个非常大的超时时间，近似于永不超时

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(HospitalUploadData $hospitalUploadData)
    {
        $this->hospitalUploadData = $hospitalUploadData;
        $this->onQueue('hospital_upload_data');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $hospitalUploadData = $this->hospitalUploadData;
        $hospitalid = $hospitalUploadData->hospital_id;
        $provider = new EnvironmentVariableCredentialsProvider();
        $bucket= config('filesystems.disks.hospitalData.bucket');
        $object = $hospitalid.'/'.$hospitalUploadData->file_name;
        $localfile = "/tmp/" . $hospitalid . ".xlsx";
        $config = array(
            "provider" => $provider,
            "endpoint" => 'oss-cn-beijing.aliyuncs.com',
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-beijing"
        );
        $ossClient = new OssClient($config);
        $options = array(
            OssClient::OSS_FILE_DOWNLOAD => $localfile
        );
        $ossClient->getObject($bucket,$object, $options);


        if (empty($localfile) OR !file_exists($localfile)) {
            $this->fail('临时文件不存在');
        }
        try {
            Excel::import(new HospitalImport($hospitalUploadData), $localfile);
            $hospitalUploadData->status = 3;
            $hospitalUploadData->save();
        }catch (\Exception $e){
            $hospitalUploadData->status = 4;
            $hospitalUploadData->save();
            $this->fail($e->getMessage());
        }
    }
    /**
     * 获取作业的唯一 ID。
     */
    public function uniqueId(): string
    {
        return $this->hospitalUploadData->id;
    }

    /**
     * 同一条数据同一时间只处理一次。
     *
     * @return array<int, object>
     */
    public function middleware(): array
    {
        return [(new WithoutOverlapping($this->hospitalUploadData->id))->dontRelease()];
    }
}
